
# 查找对应文件
file(GLOB SRC_LISTS "src/*.cpp")

#构建库
add_library(MYMATH STATIC ${SRC_LISTS})

#设置使用库外界需要搜索的头文件目录(采用cmake生成器；默认包含对应usr/local/include里的文件去搜索)

target_include_directories(MYMATH INTERFACE "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"  ) 

# 库的构建树

set_target_properties(MYMATH  PROPERTIES  ARCHIVE_OUTPUT_DIRECTORY  ${CMAKE_BINARY_DIR}/lib )

# 库的安装以及设置目标集合

include(GNUInstallDirs)#填充默认安装路径的变量
install(TARGETS MYMATH
    EXPORT MYMATHTargets # 声明对应的MYMATH导出目标集合就是这个名字
    DESTINATION ${CMAKE_INSTALL_LIBDIR} # usr/local/lib
)

# 安装对应头文件（把对应头文件搬到对应的target_include_directories说的地方

install(DIRECTORY include/ 
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/math # /usr/local/include/math/math.h
    FILES_MATCHING PATTERN "*.h" # 只安装.h结尾
)

# 导出目标到构建树（cmake..）

export(EXPORT MYMATHTargets
     FILE ${CMAKE_CURRENT_BINARY_DIR}/MYMATHTargets.cmake # 导出在当前cmake公共目录如build；转化成对应cmake脚本文件

)

# 导出目标到安装树（默认lib的安装路径）


install(EXPORT MYMATHTargets 
        FILE MYMATHTargets.cmake
         NAMESPACE math::   # 给库加上防重复
          DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/MYMATH    # usr/local/lib/cmake/MYMATH 

)


# 生成对应库的配置文件

include(CMakePackageConfigHelpers) #提供相关函数
configure_package_config_file(
    #根据Config.cmake.in为模版进构建真正的配置文件MYMATHConfig.cmake
  ${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/MYMATHConfig.cmake
  INSTALL_DESTINATION   "lib/cmake/MYMATH"  # 默认安装路径就是usr/local 然后又指明相对路径

)

# 配置文件安装



install ( FILES  ${CMAKE_CURRENT_BINARY_DIR}/MYMATHConfig.cmake DESTINATION "lib/cmake/MYMATH") # usr/local/lib/cmake



